package com.study.system.controller;

import com.study.system.dto.ResultData;
import com.study.system.entity.Club;
import com.study.system.entity.News;
import com.study.system.service.NewsService;
import com.study.system.utils.RequestUtil;
import com.study.system.vo.NewsInfoVO;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/news")
public class NewsController {

    @Resource
    private NewsService newsService;

    // 查询指定社团的所有新闻信息
    @PostMapping("/show_club_all_news")
    public ResultData<List<NewsInfoVO>> findAllClubNews(@RequestBody Club club) {
        List<NewsInfoVO> result = newsService.findAllClubNews(club.getId());
        return ResultData.success(result);
    }

    // 删除新闻
    @PostMapping("/deleteNews")
    public ResultData<Boolean> deleteNews(@RequestBody Map<String, Integer> newsIdInfo) {
        return ResultData.success(newsService.deleteNewsById(newsIdInfo.get("id")));
    }

    // 修改新闻
    @PostMapping("/modifyNews")
    public ResultData<Boolean> modifyNews(@RequestBody News newsInfo) {
        return ResultData.success(newsService.modifyNews(newsInfo.getId(), newsInfo.getTitle(), newsInfo.getContent(), newsInfo.getTime(), newsInfo.getClubId()));
    }

    // 新建新闻
    @PostMapping("/createNews")
    public ResultData<Boolean> createNews(HttpServletRequest request, @RequestBody News newsInfo) {
        return ResultData.success(newsService.createNews(RequestUtil.getUserId(request), newsInfo.getClubId(), newsInfo.getTitle(), newsInfo.getContent(), newsInfo.getTime()));
    }

    // 按标题查询
    @PostMapping("searchNewsByTitle")
    public ResultData<List<NewsInfoVO>> searchNewsByTitle(@RequestBody News news) {
        return ResultData.success(newsService.searchNewsByTitle(news));
    }
}
